#!/bin/bash
#===============================================================================
# jpfat.sh
#
# Schedule JPF instances to run on a set of servers (specified explicitly) at a
# specified time.
#===============================================================================

test -x ./config.sh && . ./config.sh &>/dev/null

# Check that the necessary configuration settings are available.

if [ "${JPF_DISTRIB_LOCAL_DIR}" == "" ] ; then
	echo "Setting \${JPF_DISTRIB_LOCAL_DIR} is not set"
	exit 1
fi

if [ "${HOST_USERNAME}" == "" ] ; then
	echo "Setting \${HOST_USERNAME} is not set"
	exit 1
fi

#-------------------------------------------------------------------------------
# Explain the command line options to the user.
#-------------------------------------------------------------------------------

function SHOW_OPTIONS() {
	EXIT_CODE=${1} ; shift

	cat <<EOF
Usage: ${0} [options] <hh:mm:ss> <jpf-target-file> [<ip>...]

Options:

  -help           Display this message and exit

EOF
	exit ${EXIT_CODE}
}

#-------------------------------------------------------------------------------
# Initialize the behaviour (before processing command line options).
#-------------------------------------------------------------------------------

AT_TIME=""
TARGET=""
IPS=""

#-------------------------------------------------------------------------------
# Process the command line options.
#-------------------------------------------------------------------------------

while [ $# -gt 0 ] ; do
	OPTION=$1 ; shift
	case ${OPTION} in
		-help) SHOW_OPTIONS 0 ;;
		-*) SHOW_OPTIONS 1 ;;
		*)	if [ "${AT_TIME}" == "" ] ; then
				AT_TIME=${OPTION}
			elif [ "${}" == "" ] ; then
				TARGET=${OPTION}
			else
				IPS="${IPS};${OPTION}"
			fi ;;
	esac
done

#-------------------------------------------------------------------------------
# Check the script behaviour settings after options have been processed.
#-------------------------------------------------------------------------------

# Set the regular expression to recognize times.
TIME_RE='^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$'

if [ "${AT_TIME}" == "" ] ; then
	echo "No time specified."
	SHOW_OPTIONS 1
fi

if ! [[ ${AT_TIME} =~ ${TIME_RE} ]] ; then
	echo "Illegal time specification."
	SHOW_OPTIONS 1
fi

if [ "${TARGET}" == "" ] ; then
	echo "No JPF target file specified."
	SHOW_OPTIONS 1
fi

TARGET_BASE=`basename ${TARGET} ".jpf"`

TARGET_JPF="${JPF_DISTRIB_LOCAL_DIR}/src/examples/${TARGET_BASE}.jpf"

if [ ! -r ${TARGET_JPF} ] ; then
	echo "JPF target file \"${TARGET_JPF}\" is not available"
	exit 1
fi

BUILD_JPF="build/examples/${TARGET_BASE}.jpf"

if [ "${IPS}" == "" ] ; then
	echo "No IPs specified"
	exit 1
fi

#-------------------------------------------------------------------------------
# Now issue the commands to schedule the instances.
#-------------------------------------------------------------------------------

N=0
for IP in `echo ${IPS} | sed -e 's/;/ /g'` ; do
	echo "[${IP}] ./jpf +search.name=${TARGET_BASE} ${BUILD_JPF} >out${N}.txt"
	echo "sh -c 'cd JPF ; ./jpf +search.name=${TARGET_BASE} ${BUILD_JPF} >out${N}.txt 2>&1'" | ssh ${HOST_USERNAME}@${IP} "cat > cmd${N}.txt"
	ssh -n -f ${HOST_USERNAME}@${IP} "at -M -f cmd${N}.txt ${AT_TIME} &>/dev/null"
	N=$((${N} + 1))
done

#===============================================================================
# End of jpfat.sh
#===============================================================================
